<?php
namespace Orm\UserRank;

use Base\Exception\RmException;
class UserRankClass extends \Base\BaseOrmObject{
    protected   $id,
				$user_id = 0, // 用户id
				$nickname = '', // 用户昵称
				$product_id = 0, // 奖品id
				$user_sort = 0, // 用户名次
				$user_score = 0, // 用户积分
                $status = 0,//发货状态
                $read = 0,//是否已经阅读
                $logistics_num = '',//物流单号
				$type = 0, // 排行分类 1-周排行 2-总排行
				$week_type = 0, // 周排行区间 1-第一周 2-第二周 3-第三周 4-第四周
				$is_del = 0, // 默认0-正常 1-删除
				$insert_time = '',
				$update_time = '';

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }

    public function loadById($id = 0)
    {
        $model = new \Orm\UserRank\UserRankModel();
        $data = $model->loadById($id);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'user_id' => $this->user_id,
			'nickname' => $this->nickname,
			'product_id' => $this->product_id,
			'user_sort' => $this->user_sort,
			'user_score' => $this->user_score,
			'type' => $this->type,
			'week_type' => $this->week_type,
            'status'=>$this->status,
            'read'=>$this->read,
            'logistics_num'=>$this->logistics_num

        );

        $model = new \Orm\UserRank\UserRankModel();
        $model->insert($data);
        $this->id = $model->getLastId();
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'user_id' => $this->user_id,
			'nickname' => $this->nickname,
			'product_id' => $this->product_id,
			'user_sort' => $this->user_sort,
			'user_score' => $this->user_score,
			'type' => $this->type,
			'week_type' => $this->week_type,
            'status'=>$this->status,
            'read'=>$this->read,
            'logistics_num'=>$this->logistics_num
        );

        $model = new \Orm\UserRank\UserRankModel();
        $model->update($data, "`id` = {$this->id}");
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\UserRank\UserRankModel();
        $model->update($data, "`id` = {$id}");
    }

    private function checkNecessaryParam()
    {
    }
}